<?php

namespace app\util;

use think\Db;
use think\Model;

/*
 * 外呼电话
 */

class Callout extends Model {
    /*
     * 鉴权接口
     */

    public function call($phone, $jobnum) {
        $accountid = "N00000033748"; //云呼账号 
        $secret = "aadd6ca0-44b8-11e9-865b-39e7f3c33f99"; //云呼密码
        $time = date("YmdHis");
        //echo $time;exit;
        $authorization = base64_encode($accountid . ":" . $time);
        $sig = strtoupper(md5($accountid . $secret . $time));
//        echo $authorization;
//        echo "<br>";

        $FromExten = $jobnum . '';
        $Exten = $phone;

        $url = "http://apis.7moor.com/v20160818/call/dialout/" . $accountid . "?sig=" . $sig;
        $data = array("FromExten" => $FromExten, "Exten" => $Exten);


//        print_r(  http_build_query($data) );exit;
//        echo $url;
//        exit;
        //echo strlen( http_build_query($data) );exit;
        //$header[] = 'Connection: Keep-Alive';
        $header[] = "Accept: application/json";
        $header[] = "Content-type: application/json;charset='utf-8'";
        $header[] = "Content-Length: " . strlen(json_encode($data));
        $header[] = "Authorization: " . $authorization;

//        echo "<pre />";
//        print_r($header);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, ($url)); //地址
        curl_setopt($ch, CURLOPT_POST, 1);   //请求方式为post
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); //post传输的数据。
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);

        //print_r($message);exit;

        $return = curl_exec($ch);

        if ($return === FALSE) {
            echo "CURL Error:" . curl_error($ch);
            exit;
        }

        curl_close($ch);
        return $return;
    }

    /*绑定小号*/
    public function midNum($midNum,$phone,$expiration) {
        $accountid = "N00000046852"; //云呼账号 
        $secret = "e817db50-ea58-11e9-bdc3-33e04bc97032"; //云呼密码
        $time = date("YmdHis");
        //echo $time;exit;
        $authorization = base64_encode($accountid . ":" . $time);
        $sig = strtoupper(md5($accountid . $secret . $time));
        //echo $authorization;
        //echo "<br>";
        $url = "http://apis.7moor.com/v20160818/rlxh/midNumBind/" . $accountid . "?sig=" . $sig;
        $data = array("midNum" => $midNum, "called" => $phone, "expiration" => $expiration);

        //print_r(  http_build_query($data) );exit;
        //echo $url;
        //exit;
        //echo strlen( http_build_query($data) );exit;
        //$header[] = 'Connection: Keep-Alive';
        $header[] = "Accept: application/json";
        $header[] = "Content-type: application/json;charset='utf-8'";
        $header[] = "Content-Length: " . strlen(json_encode($data));
        $header[] = "Authorization: " . $authorization;

        //print_r($header);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, ($url)); //地址
        curl_setopt($ch, CURLOPT_POST, 1);   //请求方式为post
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); //post传输的数据。
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        //print_r($message);exit;
        $return = curl_exec($ch);

        if ($return === FALSE) {
            echo "CURL Error:" . curl_error($ch);
            exit;
        }
        curl_close($ch);
        return $return;
    }
   
    /*解绑小号*/
    public function midNumUnBinding($mappingId,$midNum) {
        $accountid = "N00000046852"; //云呼账号 
        $secret = "e817db50-ea58-11e9-bdc3-33e04bc97032"; //云呼密码
        $time = date("YmdHis");
        //echo $time;exit;
        $authorization = base64_encode($accountid . ":" . $time);
        $sig = strtoupper(md5($accountid . $secret . $time));
        //echo $authorization;
        //echo "<br>";
        $url = "http://apis.7moor.com/v20160818/rlxh/midNumUnBinding/" . $accountid . "?sig=" . $sig;
        $data = array("mappingId" => $mappingId, "midNum" => $midNum);

        //print_r(  http_build_query($data) );exit;
        //echo $url;
        //exit;
        //echo strlen( http_build_query($data) );exit;
        //$header[] = 'Connection: Keep-Alive';
        $header[] = "Accept: application/json";
        $header[] = "Content-type: application/json;charset='utf-8'";
        $header[] = "Content-Length: " . strlen(json_encode($data));
        $header[] = "Authorization: " . $authorization;

        //print_r($header);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, ($url)); //地址
        curl_setopt($ch, CURLOPT_POST, 1);   //请求方式为post
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); //post传输的数据。
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        //print_r($message);exit;
        $return = curl_exec($ch);

        if ($return === FALSE) {
            echo "CURL Error:" . curl_error($ch);
            exit;
        }
        curl_close($ch);
        return $return;
    }
    
     /*绑定小号AXB*/
    public function midNumAXB($midNum,$called,$caller,$expiration) {
        $accountid = "N00000046852"; //云呼账号 
        $secret = "e817db50-ea58-11e9-bdc3-33e04bc97032"; //云呼密码
        $time = date("YmdHis");
        //echo $time;exit;
        $authorization = base64_encode($accountid . ":" . $time);
        $sig = strtoupper(md5($accountid . $secret . $time));
        //echo $authorization;
        //echo "<br>";
        \think\Log::info('midNum='.$midNum.'+++++++++called='.$called.'++++++caller='.$caller.'++++++expiration='.$expiration.'++++sig='.$sig.'++++icDisplayFlag=0');
        $url = "http://apis.7moor.com/v20160818/rlxh/midNumBindForAXB/" . $accountid . "?sig=" . $sig;
        $data = array("midNum" => $midNum, "called" => $called,"caller"=>$caller,"icDisplayFlag"=>'0', "expiration" => $expiration);

        //print_r(  http_build_query($data) );exit;
        //echo $url;
        //exit;
        //echo strlen( http_build_query($data) );exit;
        //$header[] = 'Connection: Keep-Alive';
        $header[] = "Accept: application/json";
        $header[] = "Content-type: application/json;charset='utf-8'";
        $header[] = "Content-Length: " . strlen(json_encode($data));
        $header[] = "Authorization: " . $authorization;

        //print_r($header);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, ($url)); //地址
        curl_setopt($ch, CURLOPT_POST, 1);   //请求方式为post
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); //post传输的数据。
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        //print_r($message);exit;
        $return = curl_exec($ch);

        if ($return === FALSE) {
            echo "CURL Error:" . curl_error($ch);
            exit;
        }
        curl_close($ch);
        return $return;
    }

}
